//!
/**
 ***********************************************************************************************************
 * <RBRenderPassShadow Implementation>
 ***********************************************************************************************************
 * @file RBRenderPassShadow.cpp
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Precompiled Header
 ***********************************************************************************************************/
#include "RBmainPCH.h"

/***********************************************************************************************************
 * Includes
 ***********************************************************************************************************/
#include "RBHardwareBufferManager.h"
#include "RBRenderer.h"
#include "RBRenderPassShadow.h"
#include "RBTexture.h"

#include "RBRenderProcess/RBEffectFactory.h"
#include "RBRenderProcess/RBEffect.h"
#include "RBRenderProcess/RBEffectVariable.h"
#include "RBRenderProcess/RBDepthStencilView.h"
#include "RBRenderProcess/RBRenderTargetView.h"

/***********************************************************************************************************
 * Defines
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Classes implementation
 ***********************************************************************************************************/

#if 0


/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBRenderPassShadow::RBRenderPassShadow(const RBString& strTechniqueName, RBCamera* pShadowCamera, uint32 uiWidth, uint32 uiHeight, ShadowType eType /* = ESM */)
: m_eType(eType)
{
    UNUSED(strTechniqueName);

    m_pShadowEffect = RBEffectFactory::GetSingleton()->RetrieveEffect(strTechniqueName.c_str());

    Culler = new RBCuller;
    Culler->SetCamera(pShadowCamera);
    
    RBPixelFormat eFBFormat = PF_R32_FLOAT;

    bool bColorIsTexture = true;//(eType != HSM);
    bool bDSIsTexture = false;//!bColorIsTexture;

    RenderTargetGroup = RBRenderTargetGroup::Create(uiWidth, uiHeight, eFBFormat, PF_D24_UNORM_S8_UINT, bColorIsTexture, bDSIsTexture, 1, 0, 1);

    m_pTexture = RBRenderer::GetSingleton()->GetHBMgr()->CreateShaderResourceView((m_eType == RBRenderPassShadow::HSM) ? RenderTargetGroup->GetDepthStencilView()->GetBuffer() : RenderTargetGroup->GetRenderTargetView()->GetBuffer());

}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBRenderPassShadow::~RBRenderPassShadow()
{

}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
void RBRenderPassShadow::PostProcessGeometries( const RBGeometryArray& geometries, RBVisibilityComponentArray& entities )
{
    RBRenderPass::DefaultPostProcessGeometries(geometries, entities);
    RBTextureSampler sampler;

    for (uint32 ui=0; ui<geometries.size(); ++ui)
    {
        geometries[ui]->GetMaterial()->GetSampler("SHADOWBUFFER").Texture = m_pTexture;
        geometries[ui]->GetMaterial()->GetSampler("SHADOWBUFFER").MinFilter = RBSamplerState::SFM_POINT;
        geometries[ui]->GetMaterial()->GetSampler("SHADOWBUFFER").MagFilter = RBSamplerState::SFM_POINT;
        geometries[ui]->GetMaterial()->GetSampler("SHADOWBUFFER").MipFilter = RBSamplerState::SFM_POINT;
        geometries[ui]->GetMaterial()->SetTechnique(0);
    }
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBRenderPassShadow::PreProcessGeometries( const RBVisibilityComponentArray& entities, RBGeometryArray& geometries )
{
    RBRenderPass::DefaultPreProcessGeometries(entities, geometries);
    for (uint32 ui=0; ui<geometries.size(); ++ui)
    {
        geometries[ui]->GetMaterial()->SetTechnique(1);
    }
    void* pNative[16] = {0};
    RBRenderer::GetSingleton()->PSSetShaderResources(0, 16, pNative);
}

#endif